package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * 餐品实现类
 *
 * @author guozh
 */
@Service
public class DishServiceImpl implements DishService {


    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;



    /**
     * 分页查询
     *
     * @param dishPageQueryDTO 分页查询参数
     * @return Result<Page < Dish>>
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.selectPageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 菜品批量删除
     *
     * @param ids id
     */
    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {
        //判断当前菜品是否在起售状态
        for (Long id : ids) {
            Dish dish = dishMapper.selectDishById(id);
            if (dish.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断当前菜品是否和套餐相关联
        List<Long> setmealIds = setmealMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && !setmealIds.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品数据
        dishMapper.deleteById(ids);
        //删除菜品关联的口味数据
        dishFlavorMapper.deleteByDishId(ids);

    }

    /**
     * 根据id查询菜品
     *
     * @param id id
     * @return DishVO
     */
    @Override
    public DishVO getDishAndFlavorById(Long id) {
        Dish dish = dishMapper.selectDishById(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);

        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO 菜品信息
     */
    @Override
    public void update(DishDTO dishDTO) {
        //重新封装
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //修改菜品
        dishMapper.update(dish);

        //删除口味
        dishFlavorMapper.deleteByDishId(Collections.singletonList(dishDTO.getId()));

        //重新添加口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertFlavors(flavors);
        }
    }

    /**
     * 修改状态
     *
     * @param status 状态
     * @param id     id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.update(dish);

        //如果是停售操作对应的套餐也需要停售
        if (status.equals(StatusConstant.DISABLE)) {
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            List<Long> setmealIds = setmealMapper.getSetmealIdsByDishIds(dishIds);
            if (setmealIds != null && !setmealIds.isEmpty()) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }

    /**
     * 根据分类查询
     *
     * @param categoryId id
     * @return Result<List < Dish>>
     */
    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        Dish dish = Dish.builder().status(StatusConstant.ENABLE).categoryId(categoryId).build();
        return dishMapper.selectCategoryId(dish);
    }

    /**
     * 新增菜品和口味
     *
     * @param dishDTO 菜品数据
     */
    @Override
    @Transactional
    public void insertDishAndFlavor(DishDTO dishDTO) {
        //封装
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //增加菜品
        dishMapper.insertDish(dish);

        //添加菜品口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //生成id值
        Long dishId = dish.getId();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(dishFlavor -> {
                        dishFlavor.setDishId(dishId);
                    }
            );
            dishFlavorMapper.insertFlavors(flavors);
        }

    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish dish
     * @return List<DishVO>
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.selectCategoryId(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


}
